define(['store'], function (store) {
    'use strict';
    function ctrl($scope, $state, $stateParams, $ionicLoading, $timeout, DeviceService, CommonService, IdentificationService) {
        function init() {
            $scope.status = false;
            $scope.identification = {
                photo: {
                    positive: '../img/id_positive.png',
                    negative: '../img/id_negative.png'
                },
                status: true
            };
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });
        $scope.getPhoto = function (type) {
            DeviceService.showSelectDevice(function (data) {
                if (data.result && data.result.base64) {
                    $timeout(function () {
                        type == 'positive' ? $scope.identification.photo.positive = data.result.base64 : $scope.identification.photo.negative = data.result.base64;
                        if ($scope.identification.photo.positive.indexOf('.png') < 0 && $scope.identification.photo.negative.indexOf('.png') < 0) {
                            $scope.identification.status = false;
                        }
                    }, 1000);
                }
            });
        };
        $scope.submit = function () {
            getPhotoInfo($scope.identification.photo.positive.substring(22), '6A', function () {
                getPhotoInfo($scope.identification.photo.negative.substring(22), '6B', null)
            })
        };

        function getPhotoInfo(imgData, type, callback) {
            $ionicLoading.show();
            IdentificationService.uploadIDCard({
                image_no: type,
                image_data: imgData
            }).then(function (data) {
                    $ionicLoading.hide();
                    if (data.error_no != 0) {
                        CommonService.showAlert({message: data.error_info});
                        return;
                    }
                    if (type == '6A') {
                        //store.set('idCard1', {addr: data.id_address});
                        callback();
                    }
                    if (type == '6B') {
                        /* store.set('idCard2', {
                            policeorg: data.issued_depart,
                            idbegindate: data.id_begindate,
                            idenddate: data.id_enddate
                        });*/
                        var needVideo=store.get('needVideo');
                        var exchange_kind=store.get('exchange_kind');
                        if(needVideo){//跳转视频
                            store.set('exchange_kind',  exchange_kind);
                            $state.go('tab.account-openAccountVideo', {typeid: $stateParams.typeid});
                        }else{ //提交开户
                            var params = {exchange_kind: exchange_kind, sc_open_flag: 1};
                            $ionicLoading.show();
                            WebService.addStockAccount(params).then(function (data) {
                                $ionicLoading.hide();
                                if (data.error_info) {
                                    $scope.openAccount.status = 1;
                                    $scope.openAccount.subtext = data.error_info;
                                } else {
                                    CommonService.showComplete('../view/template/accounttemplate.html', '返回首页', 'tab.account-index');
                                }
                            },function(){
                                $ionicLoading.hide();
                                CommonService.showAlert({message: "申请失败"});
                            });
                        }
                    }
                }, function (result) {
                    $ionicLoading.hide();
                    CommonService.showAlert({message: result.error_info});
                }
            );
        }
    }

    ctrl.$inject = ['$scope', '$state', '$stateParams', '$ionicLoading', '$timeout', 'DeviceService', 'CommonService', 'IdentificationService'];
    return ctrl;
});